import shelve
from collections import Counter

article_db = shelve.open('stock_articles.she')

cross_article_wordcount = Counter()
for count, stock in enumerate(article_db):
    print 'Counting words for stock %d out of %d (%s, has %d articles)' % (count + 1, len(article_db), stock, len(article_db[stock]))
    if len(article_db[stock]) > 0:
        for article in article_db[stock]:
            cross_article_wordcount += Counter(article['wordcounts'].keys())
